package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.Customers;
import com.jf3q.sup.dto.CustomerDto;
import com.jf3q.sup.service.CustomersService;
import com.jf3q.sup.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Classname CustomersController
 * @Date 2024/3/27 9:41
 * @Created 叶超
 */
@RestController
@RequestMapping("/customers")
public class CustomersController {
    @Autowired
    CustomersService customersService;

    @PreAuthorize("hasAuthority('/customers/delete')")
    @GetMapping("/remove/{id}")
    public ResultVo remove(@PathVariable Integer id){

        try {
            customersService.removeById(id);
            return ResultVo.success("删除客户成功", null);
        } catch (Exception e) {
            throw new RuntimeException("删除客户失败");
        }
    }

    @PreAuthorize("hasAuthority('/customers/edit')")
    @PutMapping("/update")
    public ResultVo update(@RequestBody Customers customers) {
        customers.setUpdatedAt(new Date());
        try {
            customersService.updateById(customers);
            return ResultVo.success("修改客户成功", null);
        } catch (Exception e) {
            throw new RuntimeException("修改客户失败");
        }

    }

    @PreAuthorize("hasAuthority('/customers/add')")
    @PostMapping("/save")
    public ResultVo save(@RequestBody Customers customers) {
        Customers one = customersService.getOne(new LambdaQueryWrapper<Customers>().eq(StringUtils.hasText(customers.getEmail()),Customers::getEmail, customers.getEmail()));
        if (one!=null){
            return ResultVo.error("此邮箱已存在");
        }

        customers.setCreatedAt(new Date());
        customers.setUpdatedAt(new Date());
        try {
            customersService.save(customers);
            return ResultVo.success("添加客户成功", null);
        } catch (Exception e) {
            throw new RuntimeException("添加客户失败");
        }

    }

    @PreAuthorize("hasAuthority('/customers/getPage')")
    @PostMapping("/getPage")
    public ResultVo getPage(@RequestBody CustomerDto dto) {
        LambdaQueryWrapper<Customers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getCustomers().getName()), Customers::getName, dto.getCustomers().getName());
        queryWrapper.like(StringUtils.hasText(dto.getCustomers().getEmail()), Customers::getEmail, dto.getCustomers().getEmail());
        queryWrapper.like(StringUtils.hasText(dto.getCustomers().getPhone()), Customers::getPhone, dto.getCustomers().getPhone());
        queryWrapper.like(StringUtils.hasText(dto.getCustomers().getAddress()), Customers::getAddress, dto.getCustomers().getAddress());
        Page<Customers> page = customersService.page(new Page<Customers>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        return ResultVo.success("", page);
    }


}
